<?php

$range_id = $path[3];

if (ctype_digit($range_id) and $range = $db->fetchRow("SELECT * FROM shop_ranges WHERE id=? AND site_id=?", array($range_id, $site['id']))) {
	
	if (isset($_POST['range'])) {
		
		$errors = array();
		
		if (!$_POST['range']['title']) $errors[] = 'TITLE';
		if ($db->fetchOne("SELECT id FROM shop_ranges WHERE title=? AND id!=? AND site_id=?", array($_POST['range']['title'], $range['id'], $site['id']))) $errors[] = 'TITLE_UNIQUE';
		if (sizeof($errors)) {
			$smarty->assign('errors', $errors);
		} else {
			$smarty->assign('success', true);
			$db->update('shop_ranges', $_POST['range'], $db->quoteInto('id=?', $range_id));
			
		}
		
	}
	
	
	$smarty->assign('range', $range);

	if (is_xhr()) {
		header('Content-type: text/xml');
		$smarty->display('shop/ranges/_edit_messages.tpl.taconite');
	} else {             
		$smarty->display('shop/ranges/edit.tpl.html');
	}
	
} else {
	header("Location: /admin/shop/ranges/");
}
